package library.config;

import java.io.IOException;
import java.io.InputStream;

import library.mapper.BookMapper;
import library.mapper.IdentityMapper;
import library.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
    private static ThreadLocal<SqlSession> tl=new ThreadLocal<>();
    private static SqlSessionFactory ssf;
    static{
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            ssf = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        return ssf.openSession();
    }
    public static void closeSqlSession(){
        SqlSession sqlSession = tl.get();
        if(sqlSession!=null){
            sqlSession.close();
            tl.set(null);
        }
    }

}

